require(['jquery'],function(){
	var inp = jQuery("#commentform :input[tabindex]");
	jQuery(inp).bind("keydown", function (e){
		if(e.which == 13){
			e.preventDefault();
			var nxtIdx = jQuery(inp).index(this) + 1;
        	jQuery("#commentform :input[tabindex]:eq(" + nxtIdx + ")").focus();
		}
	});
})

/** submit */
function onError(info){
	$('#loading').slideUp();
	$('#error').slideDown().html(info);
	setTimeout(function() {$('#submit').attr('disabled', false).fadeTo('slow', 1); $('#error').slideUp();}, 2000);
};
function onSuccess(info){
	$('#loading').slideUp();
	$('#success').slideDown().html(info);
	setTimeout(function() {$('#submit').attr('disabled', false).fadeTo('slow', 1); $('#success').slideUp();}, 2000);
	$('textarea').each(function() {this.value = ''});
};

function check(){
	$('#loading').slideDown();
	$('#submit').attr('disabled', true).fadeTo('slow', 0.5);
	
	if(!$('#NICKNAME').val() || !$('#MOBILE').val()){
		onError("提示：必须填写昵称及手机");
		return false;
	}

	if($('#EMAIL').val() && !/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test($("#EMAIL").val())){
		onError("提示：请输入正确的邮箱");
		return false;
	}

	if(!/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test($("#MOBILE").val())){
		onError("提示：请输入正确的手机号");
		return false;
	}


	$.ajax({
		url: '/c/pub/command',
		data: $('#commentform').serialize(),
		type: $('#commentform').attr('method'),

		error: function(data) {
			data = data.evalJSON();
		},

		success: function(data) {
			data = JSON.parse(data);
			if(data.code < 0 ){
				$('#loading').slideUp();
				$('#error').slideDown().html(data.message);
				setTimeout(function() {$('#submit').attr('disabled', false).fadeTo('slow', 1); $('#error').slideUp();}, 2000);
			}else{
				$('#loading').slideUp();
				$('#success').slideDown().html(data.message);
				setTimeout(function() {$('#submit').attr('disabled', false).fadeTo('slow', 1); $('#success').slideUp();}, 2000);
				$('textarea').each(function() {this.value = ''});
			}
		}
	}); // end Ajax
  return false;
}